package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.OrderSetting;
import com.itheima.utils.DateUtils;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Resource
    private OrderSettingDao orderSettingDao;

    /**
     * 批量导入预约设置数据
     * @param orderSettingList
     */
    @Override
    public void add(List<OrderSetting> orderSettingList) {
        if (orderSettingList!=null&&orderSettingList.size()>0){
            for (OrderSetting orderSetting : orderSettingList) {
                //判断当前日期是否存在
                long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (count>0){
                    //存在就执行修改
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                }else {
                    //不存在就添加
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    @Override
    public List<Map> getOrdersettingByMonth(String date) {
        String beginDate = date+"-01";
        String endDate = date+"-31";
        List<OrderSetting> orderSettingList = orderSettingDao.getOrdersettingByMonth(beginDate,endDate);

        List<Map> data = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettingList) {
            Map map = new HashMap();
            map.put("date",orderSetting.getOrderDate().getDate());//获得几号
            map.put("number",orderSetting.getNumber());            //可预约人数
            map.put("reservations",orderSetting.getReservations());//已预约人数

            data.add(map);
        }
        return data;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        //判断当前日期是否有设置过可预约人数
        long count = orderSettingDao.findNumberByDate(orderSetting.getOrderDate());
        if (count>0) {
            //设置过可预约人数,修改可预约人数
            orderSettingDao.editNumberByDate(orderSetting);
        }else {
            //未置过可预约人数,添加预约人数
            orderSettingDao.add(orderSetting);
        }
    }

    /**
     * 分页查询
     *
     * @param
     * @return
     */
    /*@Override
    public PageResult findOrdersPage(Map map) {
        Integer currentPage = (Integer) map.get("currentPage");
        Integer pageSize = (Integer) map.get("pageSize");
        Map queryString = (Map) map.get("queryString");


        // 开启分页插件,把参数传递给插件,这样在执行 id="findByCondition"sql语句的时候,
        // 该插件会自动在该sql语句后面追加上limit ? ?..
        PageHelper.startPage(currentPage, pageSize);

        Page<Map> page = orderSettingDao.findByCondition(queryString);


        List<Map> result = page.getResult();
        boolean value = false;
        for (Map map1 : result) {
            String orderStatus = (String) map1.get("orderStatus");
            if (!orderStatus.equals("未到诊")) {
                value = true;
            }
            map1.put("value", value);
        }

        return new PageResult(page.getTotal(), page.getResult());

    }*/


    @Override
    public PageResult findOrdersPage(QueryPageBean queryPageBean) throws Exception {
        Integer page = queryPageBean.getCurrentPage();
        Integer size = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        Map<String, Object> map = JSON.parseObject(queryString, Map.class);

        if (map != null && map.size() > 0) {

            List<String> orderDate = (List<String>) map.get("orderDate");

            if (orderDate != null && orderDate.size() > 0) {
                Date begin = DateUtils.parseString2Date(orderDate.get(0));
                Date end = DateUtils.parseString2Date(orderDate.get(1));
                map.put("begin", begin);
                map.put("end", end);
            }

        }
        Page<Object> pages = PageHelper.startPage(page, size);

        orderSettingDao.findByCondition(map);

        long total = pages.getTotal();

        System.out.println(total);
        List<Object> result = pages.getResult();
        return new PageResult(total,result);
    }
}
